<template>
	<view class="content">
		<view class="top_bar">
			<view class="left ml-12">
				<view class="orderState">订单状态：</view>
				<picker class="order-picker" :range="pickerList" @change="pickerChange" :value="index">
					<view class="pickerStyle">{{pickerList[index]}}</view>
				</picker>
			</view>
			<view class="todayHome" @click="openTodayHome">今日在家</view>
		</view>
		<view class="items">
			<view class="item" v-for="(item,index) in itemList" :key="index">
				<view class="top">
					<view class="top-left">
						<img class="car-img" :src="item.imgSrc" alt="">
						<view class="carInfo ml-24">
							<view class="flex-center">
								<view class="car-name">{{item.carName}}</view>
								<view class="car-tag flex-center" :style="stateColor[item.state]">{{carState[item.state]}}</view>
							</view>
							<view class="car-license">{{item.license}}</view>
						</view>
					</view>
					<view class="flex-start" style="margin-right: 24rpx;" @click="goCarMessage">
						<view class="flex-center">
							<view class="state">查看详情</view>
							<uni-icons class="arrow" size="16" type="arrowright"></uni-icons>
						</view>
					</view>
				</view>
				<view class="line"></view>
				<view class="caozuo" @click="putOff(index)">
					<view class="cz flex-center">
						{{item.isOff?'点击上架':'点击下架'}}
					</view>
				</view>
			</view>

		</view>
		<view class="button">
			<view class="appointment" @click="addCar">
				+ 新增车辆
			</view>
		</view>
		<uni-popup ref="addNewCar" type="center">
			<scroll-view scroll-y="true" class="newcarinfo">
				<view class="baseInfo">
					<view class="car-info-box">
						<view class="title">基本信息</view>
						<view class="brand all-center">
							<view class="brand-key">品牌</view>
							<input class="brand-value" placeholder-class="p-class" type="text" v-model="carBrand" placeholder="请输入车辆品牌" />
						</view>
						<view class="brand all-center">
							<view class="brand-key">型号</view>
							<input class="brand-value" placeholder-class="p-class" type="text" v-model="carType" placeholder="请输入车辆型号" />
						</view>
						<view class="brand all-center">
							<view class="brand-key">保险状态</view>
							<input class="brand-value" placeholder-class="p-class" type="text" v-model="insuranceState" placeholder="请输入保险状态" />
						</view>
						<view class="brand all-center" style="border: 0;">
							<view class="brand-key">行驶证照片</view>
							<image class="lince-img" :src="licenseImg" mode="" @click="uploadImage"></image>
						</view>
					</view>
				</view>
				<view class="baseInfo">
					<view class="car-info-box">
						<view class="title">车辆照片</view>
						<view class="tips">主图照片上传，建议45度视角</view>
						<view class="imgs-box">
							<view class="upload-img" v-for="(item,index) in imgs" @click="chooseImg(index)" :key="index">
								<image class="lince-img" :src="item.src" mode=""></image>
							</view>
						</view>
					</view>
				</view>
				<view class="baseInfo">
					<view class="car-info-box">
						<view class="title">附加信息</view>
						<view class="brand all-center">
							<view class="brand-key">车牌</view>
							<input class="brand-value" placeholder-class="p-class" type="text" v-model="carNumber" placeholder="请输入车牌" />
						</view>
						<view class="brand all-center">
							<view class="brand-key">归属地</view>
							<input class="brand-value" placeholder-class="p-class" type="text" v-model="carAddress" placeholder="请选择归属地" />
						</view>
						<view class="brand all-center">
							<view class="brand-key">保险状态</view>
							<input class="brand-value" placeholder-class="p-class" type="text" v-model="insuranceState" placeholder="请输入保险状态" />
						</view>
						<view class="brand all-center">
							<view class="brand-key">车辆描述</view>
							<input class="brand-value" placeholder-class="p-class" type="text" v-model="carDescribe" placeholder="请输入车辆描述" />
						</view>
					</view>
				</view>
				<view class="baseInfo">
					<view class="btns">
						<view class="left-btn" @click="cancle">取 消</view>
						<view class="right-btn" @click="commit">确 认</view>
					</view>
				</view>
			</scroll-view>

		</uni-popup>
		<uni-popup ref="todayHome" type="center">
			<view class="info" @click="closeTodayHome">

			</view>
		</uni-popup>
	</view>
</template>

<script>
	import icons from "../../../common/icons.js"
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	import uniIcons from "@/components/uni-icons/uni-icons.vue"
	import request from '../../../requst.js'
	import chooseAndUploadImage from '@/js_sdk/tencentcloud-plugin-cos/choose-and-upload-image.js';
	import getFileAccessUrl from '@/js_sdk/tencentcloud-plugin-cos/get-file-access-url.js';
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		comments: {
			uniPopup,
			uniIcons
		},
		computed: {
			...mapState([
				"imgurl"
			])
		},
		data() {
			return {
				pickerList: ['全部', '可用', '在租', '下架', '审核'],
				index: 0,
				itemList: [{
						imgSrc: icons.icons.picCar,
						carName: '法拉利 888',
						state: 0,
						license: '粤B 888888',
						isOff: false,
					},
					{
						imgSrc: icons.icons.picCar,
						carName: '法拉利 888',
						state: 1,
						license: '粤B 888888',
						isOff: false,
					},
					{
						imgSrc: icons.icons.picCar,
						carName: '法拉利 888',
						state: 2,
						license: '粤B 888888',
						isOff: false,
					},
					{
						imgSrc: icons.icons.picCar,
						carName: '法拉利 888',
						state: 3,
						license: '粤B 888888',
						isOff: false,
					}
				],
				model: null,
				imgs: [{
						src: icons.icons.iconadd,
						isUpload: false,
					},
					{
						src: icons.icons.iconadd,
						isUpload: false,
					},
					{
						src: icons.icons.iconadd,
						isUpload: false,
					},
					{
						src: icons.icons.iconadd,
						isUpload: false,
					},
					{
						src: icons.icons.iconadd,
						isUpload: false,
					},
					{
						src: icons.icons.iconadd,
						isUpload: false,
					},
					{
						src: icons.icons.iconadd,
						isUpload: false,
					},
					{
						src: icons.icons.iconadd,
						isUpload: false,
					}
				],
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				carState: {
					0: '可用',
					1: '在租',
					2: '已下架',
					3: '审核中'
				},
				stateColor: {
					0: 'border: 1px solid #3C8144;color: #3C8144;',
					1: 'border: 1px solid #F7A533;color: #F7A533',
					2: 'border: 1px solid #DC3434;color: #DC3434',
					3: 'border: 1px solid #4073FF;color: #4073FF'
				},
				// licenseImg: icons.icons.iconadd,
				licenseImg: icons.icons.iconadd,
				carBrand: null,
				carType: null,
				carNumber: null,
				carAddress: null,
				carDescribe: null,
				insuranceState: null,
				currentPage:1,
				carStateIndex:null,

			}
		},
		onLoad() {
			this.getCars();
		},
		methods: {
			pickerChange(e) {
				this.index = e.detail.value;
			},
			goCarMessage() {
				uni.navigateTo({
					url: "/pages/my/carmanage/carmessage/carmessage"
				})
			},
			openTodayHome() {
				this.$refs.todayHome.open();
			},
			closeTodayHome() {
				this.$refs.todayHome.close();
			},
			addCar() {
				this.$refs.addNewCar.open();
			},
			cancle() {
				this.$refs.addNewCar.close();
			},
			confirm() {
				this.$refs.addNewCar.close();
			},
			putOff(index) {
				if (!this.itemList[index].isOff) { //false已上架，为点击下架
					uni.showModal({
						title: '是否确认下架该车辆？',
						success: res => {
							if (res.confirm) {
								this.itemList[index].isOff = !this.itemList[index].isOff;
							}
						}
					})
				} else {
					uni.showModal({
						title: '是否确认上架该车辆？',
						success: res => {
							if (res.confirm) {
								this.itemList[index].isOff = !this.itemList[index].isOff;
							}
						}
					})
				}
			},
			chooseImg(index) {
				if (this.imgs[index].isUpload) return
				uni.chooseImage({
					count: 8, //限制图片上传的数量,做多9张
					success: res => { //上传成功的回调
						console.log(res)
						for (var i = 0; i < res.tempFilePaths.length; i++) {
							this.imgs[i].src = res.tempFilePaths[i];
							this.imgs[i].isUpload = true;
						}
					}
				})
			},
			commit() {
				var file = [];
				var item = {
					photoAddress:null
				};
				for (var i = 0; i < this.imgs.length; i++) {
					if (this.imgs[i].isUpload) {
						item.photoAddress = this.imgs[i].src;
						file.push(item);
					}
				}
				request({
					url: 'qijian/personalCentre/add',
					method: 'POST',
					data: {
						carBrand: this.carBrand,
						carType: this.carType,
						carNumber: this.carNumber,
						carAddress: this.carAddress,
						carDescribe: this.carDescribe,
						insuranceState: this.insuranceState,
						carPhotoAddressEntities: file,
						vehicleLicense: this.fileName
					},
					success: data => {
						console.log('data', data)
					}
				})
			},
			license() {
				uni.chooseImage({
					count: 1,
					success: res => {
						console.log(JSON.stringify(res.tempFilePaths))
						this.licenseImg = res.tempFilePaths[0];
					}
				})
			},
			async uploadImage() {
				this.fileName = null;
				this.fileUrl = null;
				try {
					const key = await chooseAndUploadImage(); // 选择并上传图片
					this.fileName = key;
					0
					console.log(this.fileName)
				} catch (error) {
					console.log(error)
					uni.showToast({
						icon: 'none',
						title: error.message
					});
				}
			},
			getCars(){
				request({
					// ?carState=审核中&limit=10&page=1
					url:'qijian/personalCentre/carManage?limit=10'+'&page=1',
					method: 'POST',
					header:{
						'content-type' : 'application/json'
					},
					// data:{
					// 	carState:'审核中',
					// 	limit:10,
					// 	page:1,
					// },
					success: res=>{
						console.log(res);
					}
				})
			}

		}
	}
</script>

<style>
	page {
		background-color: #F0F0F0;
	}
</style>

<style scoped>
	.btns {
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 92vw;
		margin-top: 20rpx;
		padding-bottom: 40rpx;

	}

	.right-btn {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 322rpx;
		height: 88rpx;
		font-size: 36rpx;
		font-family: PingFangSC-Semibold, PingFang SC;
		font-weight: 600;
		color: #B6914E;
		background: #000000;
		border-radius: 6px;
	}

	.left-btn {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 322rpx;
		height: 88rpx;
		font-size: 36rpx;
		font-family: PingFangSC-Semibold, PingFang SC;
		font-weight: 600;
		color: #333333;
		background: #E4E4E4;
		border-radius: 6px;
	}

	.upload-img {
		margin-top: 10rpx;
	}

	.imgs-box {
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
		align-items: center;
		margin: 20rpx 0;

	}

	.tips {
		font-size: 28rpx;
		font-family: PingFang-SC-Medium, PingFang-SC;
		font-weight: 500;
		color: #666666;
		margin-top: 40rpx;
	}

	.lince-img {
		width: 158rpx;
		height: 158rpx;
	}

	.all-center {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.brand {
		padding: 20rpx 0;
		border-bottom: 1rpx solid #EBEBEB;
	}

	.brand-key {
		font-size: 28rpx;
		font-family: PingFang-SC-Medium, PingFang-SC;
		font-weight: 500;
		color: #666666;
	}

	.brand-value {
		width: 30vw;
		font-size: 28rpx;
		font-family: PingFang-SC-Medium, PingFang-SC;
		font-weight: 500;
		color: #333333;
	}

	.car-info-box {
		width: 92vw;
	}

	.title {
		margin-top: 20rpx;
		font-size: 32rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #333333;
	}

	.baseInfo {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100vw;
		margin-top: 20rpx;
		background: #FFFFFF;
	}

	.close-img {
		width: 28rpx;
		height: 28rpx;
	}

	.newcarinfo {
		width: 100vw;
		height: 100vh;
		display: flex;
		flex-direction: column;
		justify-content: flex-start;
		align-items: center;
	}

	.line {
		align-self: center;
		width: 686rpx;
		height: 1px;
		background-color: #F4F4F4;
		margin-top: 40rpx;
		margin-bottom: 40rpx;

	}

	.arrow {
		color: #B6914E;
	}

	.flex-start {
		display: flex;
		justify-content: start;
		/* align-items: center; */
	}

	.state {
		font-size: 28rpx;
		font-family: PingFang-SC-Medium, PingFang-SC;
		font-weight: 500;
		color: #B6914E;
	}

	.car-tag {
		width: 72rpx;
		height: 32rpx;
		border-radius: 3px;
		margin-left: 12rpx;
		font-size: 24rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;

	}

	.flex-center {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.car-name {
		font-size: 32rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #292929;
	}

	.car-license {
		font-size: 28rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #292929;
	}

	.car-img {
		width: 108rpx;
		height: 88rpx;
		border-radius: 3px;
	}

	.todayHome {
		display: flex;
		justify-content: center;
		align-items: center;
		background: #B6914E;
		border-radius: 3px;
		margin-right: 20rpx;
		padding: 6rpx 12rpx;
		font-size: 24rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #FFFFFF;
	}

	.pickerStyle {
		font-size: 24rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #B6914E;
		padding: 6rpx 12rpx;
	}

	.order-picker {
		background: #FFFFFF;
		border-radius: 3px;
	}

	.orderState {
		font-size: 24rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #666666;
	}

	.button {
		position: fixed;
		/* padding: 0 6rpx; */
		/* margin-bottom: 12rpx; */
		width: calc(100%);
		bottom: var(--window-bottom);
		/* z-index: 2; */
		background: #FFFFFF;
	}

	.appointment {
		width: calc(100%);
		height: 88rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 32rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #000000;
	}

	.cz {
		/* background-color: #A6A6A6; */
		width: 180rpx;
		height: 56rpx;
		border-radius: 6px;
		border: 1px solid #B6914E;
		/* background: #FFFFFF; */
		font-size: 28rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #B6914E;
		margin-right: 24rpx;
	}

	.carInfo {
		font-size: 32rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #292929;
	}

	.top-left {
		display: flex;
		justify-content: flex-start;
	}

	.caozuo {
		width: calc(100%);
		display: flex;
		justify-content: flex-end;
		margin-bottom: 24rpx;
	}

	.top {
		display: flex;
		justify-content: space-between;
		margin-top: 24rpx;
		margin-left: 24rpx;

	}

	.item {
		background: #FFFFFF;
		display: flex;
		flex-direction: column;
		margin-bottom: 12rpx;

	}

	.items {
		display: flex;
		flex-direction: column;
		margin-top: 60rpx;
		width: calc(100%);
	}

	.mr-12 {
		margin-right: 12rpx;
	}

	.ml-12 {
		margin-left: 12rpx;
	}

	.ml-24 {
		margin-left: 24rpx;
	}

	.left {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-left: 20rpx;
	}

	.right {
		width: 140rpx;
		height: 40rpx;
		background-color: #B6914E;
		padding: 6rpx 24rpx;
		border-radius: 3px;
		color: #FFFFFF;
		font-size: 24rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
	}

	.info {
		width: 100vw;
		height: 100vh;
		padding: 16rpx;
	}

	.top_bar {
		position: fixed;
		top: var(--window-top);
		left: 0;
		width: calc(100%);
		padding-top: 12rpx;
		padding-bottom: 12rpx;
		background-color: #F0F0F0;
		z-index: 2;

		display: flex;
		/* flex-direction: column; */
		justify-content: space-between;
		align-items: center;
		font-size: 24rpx;

	}

	.content {
		display: flex;
		flex: 1;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		width: calc(100%);
		/* width: 100%; */
		/* padding: 0 15rpx; */
	}
</style>
